package com.open.pokergames.comm.userCenter

import android.os.Bundle
import android.view.View
import com.open.pokergames.MainActivity
import com.open.pokergames.R
import com.open.pokergames.base.activity.EventBusTitleBaseActivity
import com.open.pokergames.comm.repository.cache.LocalCache
import com.open.pokergames.comm.repository.entity.LoginDto
import com.open.pokergames.comm.repository.entity.ResultInfo
import com.open.pokergames.comm.repository.entity.UserVo
import com.open.pokergames.comm.repository.viewModel.SystemViewModel
import com.sinothk.android.utils.XUtils
import kotlinx.android.synthetic.main.activity_register.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class RegisterActivity : EventBusTitleBaseActivity(), View.OnClickListener {
    var viewModel: SystemViewModel? = null
    private var userVo: UserVo? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_register)
        setViewTitle("注册")

        viewModel = SystemViewModel()
        userVo = UserVo()

        setListener()
    }

    private fun setListener() {
//        sexRadioGroup.setOnCheckedChangeListener { _, checkedId ->
//            when (checkedId) {
//                R.id.sexMaleRb -> {
//                    userVo?.sex = "男";
//                }
//                R.id.sexFemaleRb -> {
//                    userVo?.sex = "女";
//                }
//            }
//        }

        toRegisterBtn.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when (v) {
            toRegisterBtn -> {
                submit()
            }
        }
    }

    private fun submit() {
        val name = nameEt.text.toString()
        val phone = phoneEt.text.toString()
        val pwd = pwdEt.text.toString()
        val pwd2 = pwd2Et.text.toString()

        if (XUtils.string().isEmpty(name)) {
            XUtils.toast().show("用户名称不能为空")
            return
        }
        if (XUtils.string().isEmpty(phone)) {
            XUtils.toast().show("手机号码不能为空")
            return
        }
        if (XUtils.string().isEmpty(pwd)) {
            XUtils.toast().show("用户密码不能为空")
            return
        }
        if (XUtils.string().isEmpty(pwd2)) {
            XUtils.toast().show("密码确认不能为空")
            return
        }
        if (pwd != pwd2) {
            XUtils.toast().show("两次输入的密码不一致，请检查后重试")
            return
        }
//        if (XUtils.string().isEmpty(userVo?.sex)) {
//            XUtils.toast().show("请选择用户性别")
//            return
//        }

        userVo!!.userName = name
        userVo!!.phoneNum = phone
        userVo!!.password = pwd
        showLoadingDialog("正在注册")
        viewModel!!.register(userVo!!)
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun loginCallback(resultInfo: ResultInfo<LoginDto>) {

        if (resultInfo.eventType != "register") {
            return
        }
        hideLoadingDialog()
        when (resultInfo.code) {
            ResultInfo.SUCCESS -> {
                LocalCache.setLoginUser(resultInfo.data)
                LocalCache.setAutoLogin(true)
                XUtils.intent().openActivity(this@RegisterActivity, MainActivity::class.java)
                    .finish(true).start()
            }
            else -> {
                XUtils.toast().show(resultInfo.msg)
            }
        }
    }


}